var app = getApp(),
  fn = app.fn;

Page({
  data: {

    carts: [],
    formVal:"",
 
    addressData: {},
   
    addressStatus: 0,
    isRemote: 0,
 
    couponData: {},
   
    couponStatus: 0,
  
    total: '',
  
    combined: '',
   
    espressPrice: '',
  
    num: '',
    
    cids: '',
 
    messages: '',
    msgPrefix: '',
  },

  onLoad: function (option) {
    this.setData({
         pid: option.id,
         address_id : option.address_id
    })
    // console.log(option.address_id + "备注");
    var submitStr = 'option.submitStr';

  },


  sum: function () {
    var carts = this.data.carts;
 
    var total = 0;
  
    var num = 0;

    for (var i = 0; i < carts.length; i++) {
      total += carts[i].productNumber * carts[i].price;
      num += carts[i].productNumber;
    }

    var espressPrice = this.data.espressPrice;
    var addressData = this.data.addressData;
    var isRemote = this.data.isRemote;
    var couponData = this.data.couponData;
    var couponStatus = this.data.couponStatus;

 
    if (isRemote == 0) {
    
      espressPrice = '0';
    }

   
    var combined = 0;
    combined = total + parseInt(espressPrice);

    
    if (couponStatus == 1) {

      combined = combined - couponData.m;
      combined = parseFloat(combined).toFixed(1)
    }

 
    this.setData({
      total: total,
      combined: combined,
      espressPrice: espressPrice,
      num: num
    });
  },


  bindCheckout: function () {
    var that = this;
    var msg = that.data.msgPrefix + ':' + that.data.messages;
    console.log('111111111114');
    if (that.data.addressStatus == 0) {
      console.log('22222222222');
      app.showMsg('请设置收货地址');
      return;
    }
    console.log('33333333333');

    fn.apiData({
      url: 'addorder.php',
      data: {
        uid: that.data.uid,
        addressId: that.data.addressData.addId,
        cids: that.data.cids,
        messages: msg,
        consigneeType: '1',
        payMethod: '9',
        wallet: '0',
        couponNo: that.data.couponData.couponNo,
      },

      success: function (res) {

        console.log(res.data.result);
        if (res.data.success) {
        
          that.requestPayment(res.data.result);
        } else {
        
          app.showMsg(res.data.error_msg);
        }

      }
    });
  },


  requestPayment: function (e) {

    wx.requestPayment({
      'timeStamp': e.timestamp,
      'nonceStr': e.nonceStr,
      'package': e.package,
      'signType': e.signType,
      'paySign': e.paySign,
      'success': function (res) {
    
        app.showMsg('支付成功！');
    
        setTimeout(function () {

         
          var url;
          url = '../orderList/orderList?orderStatus=2';
          wx.redirectTo({
            url: url
          })
        }, 2000)
      },
      'fail': function (res) {
      
        app.showMsg('支付失败！');
        
        setTimeout(function () {

      
          var url;
          url = '../orderList/orderList?orderStatus=1';
          wx.redirectTo({
            url: url
          })
        }, 2000)
      }
    })
  },


  bindconfirm: function (e) {
    var that = this;
    that.setData({
      messages: e.detail.value,
    });
  },
  setFormVal: function (e) {
    var val = e.detail.value;
    this.setData({
      formVal: val
    });
  },
 
  bindAddress: function (e) {
    var url;
    var  that=this;
    that.data.formVal = this.removeAllSpace(that.data.formVal);
    url = '../firmOrder/firmOrder?id=' + that.data.pid + '&remark=' + that.data.formVal + '&address_id=' + that.data.address_id;
    // console.log(url);
    wx.navigateTo({
      url: url
    })
  },
// 去除空格
  removeAllSpace : function(str){
    return str.replace(/\s+/g,"");
  },

  addressBlock: function (e) {
    var that = this;
   
    that.setData({
      addressData: e,
      isRemote: e.isRemote,
      addressStatus: 1,
    });
   
    that.sum();
  },

  
  bindCoupon: function (e) {
    var url;
    var that = this;
    url = '../coupon/coupon?block=1&prices=' + that.data.combined;
    wx.navigateTo({
      url: url
    })
  },

 
  couponBlock: function (e) {
    var that = this;
 
    that.setData({
      couponData: e,
      couponStatus: 1,
    });
   
    that.sum();
  },



  loadData: function (e) {
    var that = this;
    fn.apiData({
      url: 'flow.php',
      data: {
        act: 'checkout',
        supplier_id: that.data.pid,
        cids: e
      },
      success: function (res) {

       
        wx.hideToast();

        if (res.data.code == 0) {
       
          that.setData({ carts: res.data.data });
        

        } else {
        
          app.showMsg(res.data.error_msg);
       
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            })
          }, 2000)
        }
      }
    });
  },

 
  loadAddressData: function () {
    var that = this;
    fn.apiData({
      url: 'defaultAddress.php',
      data: {
        uid: that.data.uid,
      },
      success: function (res) {

        if (res.data.success) {
          that.addressBlock(res.data.result);
        }
      }
    });
  },

})